﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace RaProWorld
{
    class InputHandler
    {
        protected KeyboardState previousKeyboardState;
        protected KeyboardState keyboardState;

        protected MouseState previousMouseState;
        protected MouseState mouseState;

        public Vector2 mousePos;
        
        public InputHandler()
        {
            keyboardState = Keyboard.GetState();
            mouseState = Mouse.GetState();
        }

        public void Update()
        {
            previousKeyboardState = keyboardState;
            keyboardState = Keyboard.GetState();

            previousMouseState = mouseState;
            mouseState = Mouse.GetState();

            int selectedIndex = Program.raProWorld.worldMap.currentSelectedIndex;
            
            if (Program.raProWorld.GameState == "WORLDMAP")
            {
                if (keyboardState.IsKeyDown(Keys.Escape) &&
                    !previousKeyboardState.IsKeyDown(Keys.Escape))
                    Program.raProWorld.Exit();

                if (keyboardState.IsKeyDown(Keys.Left) &&
                    !previousKeyboardState.IsKeyDown(Keys.Left))
                {
                    --selectedIndex;
                    if (selectedIndex < 0) selectedIndex = Program.raProWorld.worldMap.objects.Count - 1;
                    Program.raProWorld.worldMap.currentSelectedIndex = selectedIndex;
                    return;
                }

                if (keyboardState.IsKeyDown(Keys.Right) &&
                    !previousKeyboardState.IsKeyDown(Keys.Right))
                {
                    ++selectedIndex;
                    selectedIndex %= Program.raProWorld.worldMap.objects.Count;
                    Program.raProWorld.worldMap.currentSelectedIndex = selectedIndex;
                    return;
                }

                if (keyboardState.IsKeyDown(Keys.Enter) &&
                    !previousKeyboardState.IsKeyDown(Keys.Enter))
                {
                    Program.raProWorld.worldMap.addSubmenu();
                    Program.raProWorld.GameState = "SUBMENU";
                }

                return;
            }

            if (Program.raProWorld.GameState == "SUBMENU") 
            {
                if (keyboardState.IsKeyDown(Keys.Up) &&
                    !previousKeyboardState.IsKeyDown(Keys.Up)) 
                {
                    if (Program.raProWorld.worldMap.subMenu.status != 0)
                    {
                        Program.raProWorld.worldMap.subMenu.status--;
                    }
                }
                if (keyboardState.IsKeyDown(Keys.Down) &&
                    !previousKeyboardState.IsKeyDown(Keys.Down)) 
                {
                    if (Program.raProWorld.worldMap.subMenu.status != 2)
                    {
                        Program.raProWorld.worldMap.subMenu.status++;
                    }
                }
                if (keyboardState.IsKeyDown(Keys.Enter) &&
                    !previousKeyboardState.IsKeyDown(Keys.Enter))
                {
                    if (Program.raProWorld.worldMap.subMenu.status == 0)
                    {
                        Program.raProWorld.gameShell.runningGame = Program.raProWorld.worldMap.objects[selectedIndex].theGame;
                        Program.raProWorld.previousGameState = Program.raProWorld.GameState;
                        Program.raProWorld.GameState = "MINIGAME";
                    }
                    if (Program.raProWorld.worldMap.subMenu.status == 1)
                    {
                        Program.raProWorld.worldMap.addTextBox(Program.raProWorld.worldMap.objects[selectedIndex].description);
                        Program.raProWorld.previousGameState = Program.raProWorld.GameState;
                        Program.raProWorld.GameState = "TEXTBOX";
                    }
                    if (Program.raProWorld.worldMap.subMenu.status == 2)
                    {
                        Program.raProWorld.GameState = "WORLDMAP";
                    }
                }
                if (keyboardState.IsKeyDown(Keys.Escape) &&
                    !previousKeyboardState.IsKeyDown(Keys.Escape)) 
                {
                    Program.raProWorld.GameState = "WORLDMAP";
                }
                return;

            }

            if (Program.raProWorld.GameState == "TEXTBOX")
            {
                if (keyboardState.IsKeyDown(Keys.Escape) &&
                    !previousKeyboardState.IsKeyDown(Keys.Escape))
                {
                    Program.raProWorld.worldMap.removeTextBox();
                }

                if (keyboardState.IsKeyDown(Keys.Enter) &&
                    !previousKeyboardState.IsKeyDown(Keys.Enter))
                {
                    Program.raProWorld.worldMap.removeTextBox();
                }

                return;
            }

            if (Program.raProWorld.GameState == "MINIGAME")
            {
                Program.raProWorld.gameShell.runningGame.HandleInput();
                return;
            }

            mousePos = new Vector2(mouseState.X, mouseState.Y);
        }
    }
}